package lol.niconico.libtest.ui.paging.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.RecyclerView
import coil.api.load
import lol.niconico.libtest.R
import lol.niconico.libtest.entity.UserEntity

/**
 * Created by ZhangQianqian on 2020/7/15  16:21.
 * email 415692689@qq.com
 */
class GitHubAdapter :
    PagingDataAdapter<UserEntity, GitHubAdapter.GitHubViewHolder>(UserEntity.diffCallback) {

    override fun onBindViewHolder(holder: GitHubViewHolder, position: Int) {
        holder.tvName.text = getItem(position)?.login
        holder.avatar.load(getItem(position)?.avatar_url)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GitHubViewHolder {
        val view = inflateView(parent, R.layout.recycie_item_github)
        return GitHubViewHolder(view)
    }

    private fun inflateView(viewGroup: ViewGroup, @LayoutRes viewType: Int): View {
        val layoutInflater = LayoutInflater.from(viewGroup.context)
        return layoutInflater.inflate(viewType, viewGroup, false)
    }


    class GitHubViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvName = view.findViewById<TextView>(R.id.tvName)
        val avatar = view.findViewById<ImageView>(R.id.avatar)
    }
}